﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using One.Web.UI.WebControls;
using Resources;
using One.Web;
using Money.Interfaces;
using Money.Data;
using One.Framework;

public partial class Admin_EditCategory : System.Web.UI.Page
{
    #region fields

    private ICategoryService categoryService;
    private int categoryId;
    private Category category;
    private int success;
    private string redirectUrl = "CategoryManagement.aspx";

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        categoryService = ServiceFactory.Create<ICategoryService>();

        GetParams();
        LoadData();
        AddRules();
        if (!IsPostBack)
        {
            UpdateView();
        }
    }

    protected void AddOrUpdate(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            bool isAdd = false;
            if (category == null)
            {
                isAdd = true;
                category = new Category();
            }
            category.Name = tbName.Text;

            if (ucParent.CategoryID != 0)
            {
                category.Category1 = categoryService.GetCategoryById(ucParent.CategoryID);
            }
            else
            {
                category.Category1 = null;
            }

            if (isAdd)
            {
                categoryService.CreateCategory(category);
                success = 1;
            }
            else
            {
                categoryService.UpdateCategory(category);
                success = 2;
            }
            Response.Redirect(string.Format("{0}?success={1}", redirectUrl, success));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        categoryId = QueryStringUtility.GetInt32("categoryId", 0);
    }

    private void LoadData()
    {
        try
        {
            if (categoryId != 0)
            {
                category = categoryService.GetCategoryById(categoryId);
            }
        }
        catch
        {
            category = null;
        }
    }

    private void UpdateView()
    {
        (Master as Admin_MasterPage).SelectedTab = "categoryManagement";
        if (category == null)
        {
            pageTitle.Text = Resources.Strings.AddCategory;
            btAddOrUpdate.Text = Resources.Strings.Add;
        }
        else
        {
            pageTitle.Text = Resources.Strings.UpdateCategory;
            btAddOrUpdate.Text = Resources.Strings.Update;
            tbName.Text = category.Name;
            if (category.Category1 != null)
            {
                ucParent.CategoryID = category.Category1.Id;
                ucParent.CategoryName = category.Category1.Name;
            }
        }
    }



    private void AddRules()
    {
        validationManager.AddRule(new ValueRequiredRule(tbName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));

        validationManager.Notifier = new BalloonNotifier();
    }


    #endregion

    #region public functions


    #endregion
}
